<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/1/11
 * Time: 15:20
 */

namespace App\Http\Logics\MiniProgram;


use App\Account;
use App\Order;
use App\RechargeLog;
use App\Vip;
use App\VipCard;
use Illuminate\Support\Facades\DB;
use App\Http\Logics\BaseLogic;
class VipLogic extends BaseLogic
{
    /**
     * 创建会员
     * @param $data 会员信息
     * @return mixed 返回创建结果
     */
    public static function createVip($data)
    {
        try{
            // 通过loginSession获取user_id
            $response = self::getUserIdByLoginSession($data['loginSession']);
            if ($response->code){
                return self::response($response->code);
            }
            $user_id = $response->data;

            // 通过user_id获取partner_id
            $response = self::getPartnerIdByUserId($user_id);
            if ($response->code){
                return self::response($response->code);
            }
            $partner_id = $response->data;

            // 判断是否购买过会员卡
            $vip = Vip::where('user_id', $user_id)->first();
            if (!empty($vip)){
                $vip = Vip::where('user_id', $user_id)->where('vip_card_id', $data['vip_card_id'])->first();
                if (empty($vip))
                    return self::response(self::BUY_OTHER_VIP);
                else
                    return self::response(self::BUY_VIP_AGAIN);
            }

            // 找到要购买的会员卡，用于获取会员卡额度填入充值记录中
            $vip_card = VipCard::find($data['vip_card_id']);
            if (empty($vip_card)){
                return self::response(self::CONTENT_IS_EMPTY);
            }

            DB::transaction(function () use ($vip_card, $user_id, $partner_id, $data){
                // 创建充值记录
                $recharge_log = new RechargeLog();
                $recharge_log->change_money = $vip_card->denomination;
                $recharge_log->user_id = $user_id;
                $recharge_log->partner_id = $partner_id;
                if (!$recharge_log->save()) {
                    return self::response(self::OBJECT_SAVE_FAIL);
                }

                // 创建订单
                $order = new Order();
                $order->money = $recharge_log->change_money;
                $order->type = Order::BUY_VIP;
                $order->name = '购买会员卡';// 暂定
                if (!$order->save()) {
                    return self::response(self::OBJECT_SAVE_FAIL);
                }

                // 增加余额
                $account = Account::find($user_id);
                if (empty($account)){
                    $account = new Account();
                }
                $account->balance = $account->balance + $recharge_log->change_money;
                if (!$account->save()){
                    self::response(self::OBJECT_SAVE_FAIL);
                }

                // 没有购买过的话创建新的会员
                $vip = new Vip();
                $vip->partner_id = $partner_id;
                $vip->user_id = $user_id;
                $vip->vip_card_id = $data['vip_card_id'];
                if (!$vip->save()) {
                    return self::response(self::OBJECT_SAVE_FAIL);
                }
            });
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 获取VIP列表
     * @param $data 筛选列表
     * @return mixed 返回结果
     */
    public static function getVipList($data)
    {
        try{
            $response = self::getPartnerIdByLoginSession($data['loginSession']);
            if ($response->code){
                return self::response($response->code);
            }
            $partner_id = $response->data;

            $vip = Vip::where('partner_id', $partner_id)->get();
            foreach ($vip as $item){
                $user = $item->user()->select('nickname', 'headimgurl', 'mobile')->first();
                $item->user_nickname = $user->nickname;
                $item->user_icon = $user->headimgurl;
                $item->user_mobile= $user->mobile;
                $vip_card = $item->vipCard()->select('vip_card.name')->first();
                $item->vip_card_name = $vip_card->name;
            }
            if (empty($vip)){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            return self::response(self::SUCCESS, $vip);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }


    public static function createVipByUserId($data)
    {
        try{
            // 通过user_id获取partner_id
            $response = self::getPartnerIdByLoginSession($data['loginSession']);
            if ($response->code){
                return self::response($response->code);
            }
            $partner_id = $response->data;

            // 判断是否购买过会员卡
            $vip = Vip::where('user_id', $data['user_id'])->where('vip_card_id', $data['vip_card_id'])->first();
            if (!empty($vip)){
                return self::response(self::BUY_VIP_AGAIN);
            }
            // 没有购买过的话创建新的会员
            $vip = new Vip();
            $vip->partner_id = $partner_id;
            $vip->user_id = $data['user_id'];
            $vip->vip_card_id = $data['vip_card_id'];
            if (!$vip->save()){
                return self::response(self::OBJECT_SAVE_FAIL);
            }
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }
}